package com.sscl.blesample.adapter.provider;

import android.content.Context;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sscl.blesample.R;
import com.sscl.blesample.adapter.ServicesCharacteristicsListAdapter;
import com.sscl.blesample.adapter.entity.services_characteristics_list_entity.CharacteristicUuidItem;

/**
 * 特征UUID provider
 *
 * @author jackie
 */
public class CharacteristicUuidProvider extends BaseNodeProvider {
    @Override
    public int getItemViewType() {
        return ServicesCharacteristicsListAdapter.TYPE_CHARACTERISTIC_UUID;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_expandable_characteristic_uuid;
    }

    @Override
    public void convert(@NonNull BaseViewHolder baseViewHolder, BaseNode baseNode) {
        CharacteristicUuidItem characteristicUuidItem = (CharacteristicUuidItem) baseNode;
        baseViewHolder.setText(android.R.id.text1, characteristicUuidItem.getName())
                .setText(android.R.id.text2, characteristicUuidItem.getUuid())
                .setText(R.id.properties, getProperties(characteristicUuidItem.isCanRead(), characteristicUuidItem.isCanWrite(), characteristicUuidItem.isCanNotify()));
    }

    /**
     * 获取特征属性
     *
     * @param canRead   是否可读
     * @param canWrite  是否可写
     * @param canNotify 是否可通知
     * @return 特征属性
     */
    private String getProperties(boolean canRead, boolean canWrite, boolean canNotify) {
        Context mContext = getContext();
        if (!canRead && !canWrite && !canNotify) {
            return mContext.getString(R.string.null_);
        }
        StringBuilder stringBuilder = new StringBuilder();
        if (canRead) {
            stringBuilder.append(mContext.getString(R.string.can_read));
        }
        if (canWrite) {
            stringBuilder.append(mContext.getString(R.string.can_write));
        }
        if (canNotify) {
            stringBuilder.append(mContext.getString(R.string.can_notify));
        }
        return stringBuilder.toString();
    }
}
